#!/bin/sh

cleanup() {
  unalias nvm_type_test_alias 2>/dev/null
  unset -f type nvm_type_test_function
  case "${TEST_DIR-}" in /*) rm -rf "${TEST_DIR}" ;; esac
}
die () { echo "$@" ; cleanup ; exit 1; }

: nvm.sh
\. ../../../nvm.sh

TEST_DIR="$(mktemp -d)" || die 'unable to create temp dir'
EXE_NAME='nvm_type_test_exe'
SPACED_NAME='nvm type test spaced'
MISSING_NAME='nvm_type_test_missing_command'
for NAME in "${EXE_NAME}" nvm_type_test_function "${SPACED_NAME}"; do
  printf '#!/bin/sh\nexit 0\n' > "${TEST_DIR}/${NAME}" || die "unable to create fake executable ${NAME}"
  chmod +x "${TEST_DIR}/${NAME}" || die "unable to chmod fake executable ${NAME}"
done
PATH="${TEST_DIR}:${PATH}"

nvm_type_test_function() { :; }
alias nvm_type_test_alias='nvm_type_test_function'

# the shell's own `type`, captured before a user-defined `type` shadows it;
# its status with mixed found and missing names differs by shell
NATIVE_MISSING_FIRST="$(type "${MISSING_NAME}" cd 2>/dev/null)"
NATIVE_MISSING_FIRST_STATUS=$?
NATIVE_FOUND_FIRST="$(type cd "${MISSING_NAME}" 2>/dev/null)"
NATIVE_FOUND_FIRST_STATUS=$?
# dash quotes paths that contain spaces
NATIVE_SPACED="$(type "${SPACED_NAME}" 2>&1)"
NATIVE_SPACED_STATUS=$?
# bash outside POSIX mode does not report aliases, and its stderr names the calling line
NATIVE_ALIAS="$(type nvm_type_test_alias 2>/dev/null)"
NATIVE_ALIAS_STATUS=$?

# a user-defined `type` must be bypassed
type() {
  echo 'fake type'
  return 0
}

OUTPUT="$(nvm_type "${EXE_NAME}")" || die "executable: expected success, got $?"
EXPECTED="${EXE_NAME} is ${TEST_DIR}/${EXE_NAME}"
[ "_${OUTPUT}" = "_${EXPECTED}" ] || die "executable: got >${OUTPUT}<, expected >${EXPECTED}<"

OUTPUT="$(nvm_type "${SPACED_NAME}" 2>&1)"
STATUS=$?
[ "_${OUTPUT}" = "_${NATIVE_SPACED}" ] || die "name with spaces: got >${OUTPUT}<, expected >${NATIVE_SPACED}<"
[ "_${STATUS}" = "_${NATIVE_SPACED_STATUS}" ] || die "name with spaces: got status >${STATUS}<, expected >${NATIVE_SPACED_STATUS}<"
[ "_${STATUS}" = '_0' ] || die "name with spaces: native type failed with >${STATUS}<: >${NATIVE_SPACED}<"

OUTPUT="$(nvm_type nvm_type_test_function)" || die "function: expected success, got $?"
FIRST_LINE="$(nvm_echo "${OUTPUT}" | command sed -n 1p)"
case "${FIRST_LINE}" in
  'nvm_type_test_function is a '*function*) ;;
  *) die "function: got >${OUTPUT}<, expected >nvm_type_test_function is a ...function...<" ;;
esac
case "${OUTPUT}" in
  *"${TEST_DIR}"*) die "function: got >${OUTPUT}<, expected no mention of the executable in >${TEST_DIR}<" ;;
esac

OUTPUT="$(nvm_type nvm_type_test_alias 2>/dev/null)"
STATUS=$?
[ "_${OUTPUT}" = "_${NATIVE_ALIAS}" ] || die "alias: got >${OUTPUT}<, expected >${NATIVE_ALIAS}<"
[ "_${STATUS}" = "_${NATIVE_ALIAS_STATUS}" ] || die "alias: got status >${STATUS}<, expected >${NATIVE_ALIAS_STATUS}<"

OUTPUT="$(nvm_type cd)" || die "builtin: expected success, got $?"
case "${OUTPUT}" in
  'cd is a '*builtin) ;;
  *) die "builtin: got >${OUTPUT}<, expected >cd is a ...builtin<" ;;
esac

OUTPUT="$(nvm_type cd "${EXE_NAME}")" || die "multiple names: expected success, got $?"
EXPECTED="$(nvm_type cd)
${EXE_NAME} is ${TEST_DIR}/${EXE_NAME}"
[ "_${OUTPUT}" = "_${EXPECTED}" ] || die "multiple names: got >${OUTPUT}<, expected >${EXPECTED}<"

OUTPUT="$(nvm_type "${MISSING_NAME}" 2>/dev/null)" && die "missing: expected failure, got >${OUTPUT}<"
case "${OUTPUT}" in
  *'fake type'*) die "missing: got >${OUTPUT}< from the user-defined type" ;;
esac

OUTPUT="$(nvm_type "${MISSING_NAME}" 2>&1)"
case "${OUTPUT}" in
  *"${MISSING_NAME}"*) ;;
  *) die "missing, both streams: got >${OUTPUT}<, expected a message naming >${MISSING_NAME}<" ;;
esac

OUTPUT="$(nvm_type "${MISSING_NAME}" cd 2>/dev/null)"
STATUS=$?
[ "_${OUTPUT}" = "_${NATIVE_MISSING_FIRST}" ] || die "missing then found: got >${OUTPUT}<, expected >${NATIVE_MISSING_FIRST}<"
[ "_${STATUS}" = "_${NATIVE_MISSING_FIRST_STATUS}" ] || die "missing then found: got status >${STATUS}<, expected >${NATIVE_MISSING_FIRST_STATUS}<"

OUTPUT="$(nvm_type cd "${MISSING_NAME}" 2>/dev/null)"
STATUS=$?
[ "_${OUTPUT}" = "_${NATIVE_FOUND_FIRST}" ] || die "found then missing: got >${OUTPUT}<, expected >${NATIVE_FOUND_FIRST}<"
[ "_${STATUS}" = "_${NATIVE_FOUND_FIRST_STATUS}" ] || die "found then missing: got status >${STATUS}<, expected >${NATIVE_FOUND_FIRST_STATUS}<"

OUTPUT="$(nvm_type 2>/dev/null)"
case "${OUTPUT}" in
  *'fake type'*) die "no arguments: got >${OUTPUT}< from the user-defined type" ;;
esac

PWD_BEFORE="$(pwd)"
VARS_BEFORE=''
VARS_AFTER=''
VARS_BEFORE="$(set | command sed -n 's/^\([A-Za-z_][A-Za-z0-9_]*\)=.*/\1/p' | command sort -u)"
nvm_type cd >/dev/null || die "direct call: expected success, got $?"
VARS_AFTER="$(set | command sed -n 's/^\([A-Za-z_][A-Za-z0-9_]*\)=.*/\1/p' | command sort -u)"
PWD_AFTER="$(pwd)"
[ "_${VARS_AFTER}" = "_${VARS_BEFORE}" ] || die "direct call: variables changed, got >${VARS_AFTER}<, expected >${VARS_BEFORE}<"
[ "_${PWD_AFTER}" = "_${PWD_BEFORE}" ] || die "direct call: cwd is >${PWD_AFTER}<, expected >${PWD_BEFORE}<"
OUTPUT="$(type)"
[ "_${OUTPUT}" = '_fake type' ] || die "direct call: user-defined type got >${OUTPUT}<, expected >fake type<"

nvm_type nvm_type_test_alias >/dev/null 2>&1
OUTPUT="$(alias nvm_type_test_alias)" || die "direct alias call: the alias is gone"
case "${OUTPUT}" in
  *nvm_type_test_function*) ;;
  *) die "direct alias call: got >${OUTPUT}<, expected the alias for >nvm_type_test_function<" ;;
esac

cleanup
